package com.juyu.miliao.api.controller;

import com.alibaba.fastjson.JSONObject;
import com.juyu.miliao.api.dao.redis.usercache.UserCacheVo;
import com.juyu.miliao.api.dao.redis.usercache.UserInfoCache;
import com.juyu.miliao.api.domain.Gift;
import com.juyu.miliao.api.domain.UserWallet;
import com.juyu.miliao.api.domain.WinRecordFruitMachineInfo;
import com.juyu.miliao.api.domain.dto.FruitMachineDTO;
import com.juyu.miliao.api.domain.dto.WinInfoDTO;
import com.juyu.miliao.api.domain.vo.GiftVo;
import com.juyu.miliao.api.domain.vo.WinAnnouncementVo;
import com.juyu.miliao.api.service.FruitMachineService;
import com.juyu.miliao.api.service.GiftService;
import com.juyu.miliao.api.service.UserWalletService;
import com.juyu.miliao.common.domain.Subject;
import com.juyu.miliao.common.exception.ConsumeRuntimeException;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 水果机controller
 *
 * @author xuanqicheng
 * @create 2019-15-10:56
 */
@RestController
@RequestMapping("/fruit")
@CrossOrigin
public class FruitMachineController {

    @Autowired
    private FruitMachineService fruitMachineService;

    @Autowired
    private GiftService giftService;

    @Autowired
    private UserInfoCache userInfoCache;

    @Autowired
    private UserWalletService userWalletService;

    @GetMapping("/wallet")
    public Object getWallet(Authentication token){
        Subject subject = (Subject) token.getPrincipal();
        UserWallet userWallet = userWalletService.getUserWalletById(subject.getId());

        return userWallet;
    }

    @PostMapping("/info")
    public Object getInfo(Authentication token, @Validated FruitMachineDTO fruitMachineDTO, BindingResult bindingResult){

        if(bindingResult.hasErrors()){
            throw  new ConsumeRuntimeException(bindingResult.getFieldError().getDefaultMessage());
        }

        Subject subject = (Subject) token.getPrincipal();

        Gift gift =giftService.getGiftByIdNoCache(fruitMachineDTO.getGiftId());

        if(gift == null){
            throw  new ConsumeRuntimeException("礼物ID不正确！");
        }

        WinInfoDTO winInfoDTO = fruitMachineService.getLuckDrawInfo(fruitMachineDTO,subject.getId(),gift);

        return winInfoDTO;
    }

    @GetMapping("/win/info")
    public Object getWinInfo(){
        List<WinAnnouncementVo> resultList = new ArrayList<>();

        List<String> list = fruitMachineService.getWinInfo(100);

        for(String value : list){

            WinRecordFruitMachineInfo obj = JSONObject.parseObject(value,WinRecordFruitMachineInfo.class);

            UserCacheVo userCacheVo = userInfoCache.getUserCacheInfo(obj.getUserId());

            Gift gift = giftService.getGiftByIdNoCache(obj.getGiftId());

            WinAnnouncementVo vo = new WinAnnouncementVo()
                    .setGiftName(gift.getGiftName())
                    .setMultiple(obj.getMultiplie())
                    .setUserName(userCacheVo.getNickName())
                    .setGold(obj.getGold());
            resultList.add(vo);
        }
        return resultList;
    }


    @GetMapping("/gift")
    public Object getGiftList(){
        List<GiftVo> resultList = new ArrayList<>();
        List<Integer> list=fruitMachineService.getGiftIdList();
        for(int giftId : list){
            Gift gift = giftService.getGiftByIdNoCache(giftId);

            GiftVo giftVo = new GiftVo();

            BeanUtils.copyProperties(gift,giftVo);

            resultList.add(giftVo);
        }
        return resultList;
    }
}
